package com.lx.es.query.test.service;

import cn.lx.es.query.search.entity.PageList;
import cn.lx.es.query.search.entity.SearchParam;
import cn.lx.es.query.search.service.EsSearchService;
import com.lx.es.query.test.entity.BidprcuDrugInfoSearchDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 基础数据药品信息搜索服务
 * 使用改造后的 BidprcuDrugInfoSearchDTO
 * 
 * @author test
 * @date 2025-01-02
 */
@Service
public class BidprcuDrugSearchService {

    @Autowired
    private EsSearchService<BidprcuDrugInfoSearchDTO> esSearchService;

    /**
     * 基础搜索示例
     */
    public PageList<BidprcuDrugInfoSearchDTO> searchDrugs(BidprcuDrugInfoSearchDTO searchDTO) {
        // 设置分页
        searchDTO.setCurrentPage(1);
        searchDTO.setPageSize(10);
        
        // 执行搜索
        SearchParam<BidprcuDrugInfoSearchDTO> searchParam = new SearchParam<>(searchDTO);
        return esSearchService.search(searchParam, BidprcuDrugInfoSearchDTO.class);
    }

    /**
     * 按药品名称搜索（分词匹配）
     */
    public PageList<BidprcuDrugInfoSearchDTO> searchByDrugName(String drugName) {
        BidprcuDrugInfoSearchDTO searchDTO = new BidprcuDrugInfoSearchDTO();
        searchDTO.setDrugName(drugName);
        searchDTO.setCurrentPage(1);
        searchDTO.setPageSize(20);
        
        SearchParam<BidprcuDrugInfoSearchDTO> searchParam = new SearchParam<>(searchDTO);
        return esSearchService.search(searchParam, BidprcuDrugInfoSearchDTO.class);
    }

    /**
     * 按药品代码搜索（精确匹配 + 前缀查询）
     */
    public PageList<BidprcuDrugInfoSearchDTO> searchByDrugCode(String drugCode) {
        BidprcuDrugInfoSearchDTO searchDTO = new BidprcuDrugInfoSearchDTO();
        searchDTO.setDrugCode(drugCode);
        searchDTO.setCurrentPage(1);
        searchDTO.setPageSize(20);
        
        SearchParam<BidprcuDrugInfoSearchDTO> searchParam = new SearchParam<>(searchDTO);
        return esSearchService.search(searchParam, BidprcuDrugInfoSearchDTO.class);
    }

    /**
     * 按通用名称搜索（分词匹配）
     */
    public PageList<BidprcuDrugInfoSearchDTO> searchByGenname(String genname) {
        BidprcuDrugInfoSearchDTO searchDTO = new BidprcuDrugInfoSearchDTO();
        searchDTO.setGenname(genname);
        searchDTO.setCurrentPage(1);
        searchDTO.setPageSize(20);
        
        SearchParam<BidprcuDrugInfoSearchDTO> searchParam = new SearchParam<>(searchDTO);
        return esSearchService.search(searchParam, BidprcuDrugInfoSearchDTO.class);
    }

    /**
     * 按剂型搜索（精确匹配）
     */
    public PageList<BidprcuDrugInfoSearchDTO> searchByDosageForm(String dosformName) {
        BidprcuDrugInfoSearchDTO searchDTO = new BidprcuDrugInfoSearchDTO();
        searchDTO.setDosformName(dosformName);
        searchDTO.setCurrentPage(1);
        searchDTO.setPageSize(20);
        
        SearchParam<BidprcuDrugInfoSearchDTO> searchParam = new SearchParam<>(searchDTO);
        return esSearchService.search(searchParam, BidprcuDrugInfoSearchDTO.class);
    }

    /**
     * 按给药途径搜索（精确匹配）
     */
    public PageList<BidprcuDrugInfoSearchDTO> searchByRoute(String ruteName) {
        BidprcuDrugInfoSearchDTO searchDTO = new BidprcuDrugInfoSearchDTO();
        searchDTO.setRuteName(ruteName);
        searchDTO.setCurrentPage(1);
        searchDTO.setPageSize(20);
        
        SearchParam<BidprcuDrugInfoSearchDTO> searchParam = new SearchParam<>(searchDTO);
        return esSearchService.search(searchParam, BidprcuDrugInfoSearchDTO.class);
    }

    /**
     * 按药理分类搜索（多值匹配）
     */
    public PageList<BidprcuDrugInfoSearchDTO> searchByPhamType(String phamTypeNames) {
        BidprcuDrugInfoSearchDTO searchDTO = new BidprcuDrugInfoSearchDTO();
        searchDTO.setPhamTypeName1(phamTypeNames); // 支持逗号分隔: "抗生素,抗病毒"
        searchDTO.setCurrentPage(1);
        searchDTO.setPageSize(20);
        
        SearchParam<BidprcuDrugInfoSearchDTO> searchParam = new SearchParam<>(searchDTO);
        return esSearchService.search(searchParam, BidprcuDrugInfoSearchDTO.class);
    }

    /**
     * 按规格名称搜索（分词匹配）
     */
    public PageList<BidprcuDrugInfoSearchDTO> searchBySpecName(String specName) {
        BidprcuDrugInfoSearchDTO searchDTO = new BidprcuDrugInfoSearchDTO();
        searchDTO.setSpecName(specName);
        searchDTO.setCurrentPage(1);
        searchDTO.setPageSize(20);
        
        SearchParam<BidprcuDrugInfoSearchDTO> searchParam = new SearchParam<>(searchDTO);
        return esSearchService.search(searchParam, BidprcuDrugInfoSearchDTO.class);
    }

    /**
     * 按生产企业搜索（分词匹配）
     */
    public PageList<BidprcuDrugInfoSearchDTO> searchByManufacturer(String prodentpName) {
        BidprcuDrugInfoSearchDTO searchDTO = new BidprcuDrugInfoSearchDTO();
        searchDTO.setProdentpName(prodentpName);
        searchDTO.setCurrentPage(1);
        searchDTO.setPageSize(20);
        
        SearchParam<BidprcuDrugInfoSearchDTO> searchParam = new SearchParam<>(searchDTO);
        return esSearchService.search(searchParam, BidprcuDrugInfoSearchDTO.class);
    }

    /**
     * 按代理企业搜索（分词匹配）
     */
    public PageList<BidprcuDrugInfoSearchDTO> searchByProxyEnterprise(String prxyEntpName) {
        BidprcuDrugInfoSearchDTO searchDTO = new BidprcuDrugInfoSearchDTO();
        searchDTO.setPrxyEntpName(prxyEntpName);
        searchDTO.setCurrentPage(1);
        searchDTO.setPageSize(20);
        
        SearchParam<BidprcuDrugInfoSearchDTO> searchParam = new SearchParam<>(searchDTO);
        return esSearchService.search(searchParam, BidprcuDrugInfoSearchDTO.class);
    }

    /**
     * 按产品类型搜索（精确匹配）
     */
    public PageList<BidprcuDrugInfoSearchDTO> searchByProductType(String prodTypeName) {
        BidprcuDrugInfoSearchDTO searchDTO = new BidprcuDrugInfoSearchDTO();
        searchDTO.setProdTypeName(prodTypeName);
        searchDTO.setCurrentPage(1);
        searchDTO.setPageSize(20);
        
        SearchParam<BidprcuDrugInfoSearchDTO> searchParam = new SearchParam<>(searchDTO);
        return esSearchService.search(searchParam, BidprcuDrugInfoSearchDTO.class);
    }

    /**
     * 按批准文号搜索（精确匹配 + 通配符查询）
     */
    public PageList<BidprcuDrugInfoSearchDTO> searchByApprovalNumber(String aprvno) {
        BidprcuDrugInfoSearchDTO searchDTO = new BidprcuDrugInfoSearchDTO();
        searchDTO.setAprvno(aprvno);
        searchDTO.setCurrentPage(1);
        searchDTO.setPageSize(20);
        
        SearchParam<BidprcuDrugInfoSearchDTO> searchParam = new SearchParam<>(searchDTO);
        return esSearchService.search(searchParam, BidprcuDrugInfoSearchDTO.class);
    }

    /**
     * 按注册证号搜索（精确匹配 + 通配符查询）
     */
    public PageList<BidprcuDrugInfoSearchDTO> searchByRegistrationNumber(String regcertno) {
        BidprcuDrugInfoSearchDTO searchDTO = new BidprcuDrugInfoSearchDTO();
        searchDTO.setRegcertno(regcertno);
        searchDTO.setCurrentPage(1);
        searchDTO.setPageSize(20);
        
        SearchParam<BidprcuDrugInfoSearchDTO> searchParam = new SearchParam<>(searchDTO);
        return esSearchService.search(searchParam, BidprcuDrugInfoSearchDTO.class);
    }

    /**
     * 按日均使用量范围搜索
     */
    public PageList<BidprcuDrugInfoSearchDTO> searchByDayUseRange(BigDecimal minDayUse, BigDecimal maxDayUse) {
        BidprcuDrugInfoSearchDTO searchDTO = new BidprcuDrugInfoSearchDTO();
        // 注意：这里需要设置Range对象，但BidprcuDrugInfoSearchDTO中的dayuse是BigDecimal类型
        // 实际使用时需要根据RangeSearch注解的处理器实现来调整
        searchDTO.setCurrentPage(1);
        searchDTO.setPageSize(20);
        
        SearchParam<BidprcuDrugInfoSearchDTO> searchParam = new SearchParam<>(searchDTO);
        return esSearchService.search(searchParam, BidprcuDrugInfoSearchDTO.class);
    }

    /**
     * 搜索基药
     */
    public PageList<BidprcuDrugInfoSearchDTO> searchEssentialDrugs() {
        BidprcuDrugInfoSearchDTO searchDTO = new BidprcuDrugInfoSearchDTO();
        searchDTO.setEssdrugFlag("1"); // 假设1表示基药
        searchDTO.setCurrentPage(1);
        searchDTO.setPageSize(50);
        
        SearchParam<BidprcuDrugInfoSearchDTO> searchParam = new SearchParam<>(searchDTO);
        return esSearchService.search(searchParam, BidprcuDrugInfoSearchDTO.class);
    }

    /**
     * 搜索医保药品
     */
    public PageList<BidprcuDrugInfoSearchDTO> searchMedicalInsuranceDrugs() {
        BidprcuDrugInfoSearchDTO searchDTO = new BidprcuDrugInfoSearchDTO();
        searchDTO.setHiType("1"); // 假设1表示医保药品
        searchDTO.setCurrentPage(1);
        searchDTO.setPageSize(50);
        
        SearchParam<BidprcuDrugInfoSearchDTO> searchParam = new SearchParam<>(searchDTO);
        return esSearchService.search(searchParam, BidprcuDrugInfoSearchDTO.class);
    }

    /**
     * 搜索非黑名单药品
     */
    public PageList<BidprcuDrugInfoSearchDTO> searchNonBlacklistDrugs() {
        BidprcuDrugInfoSearchDTO searchDTO = new BidprcuDrugInfoSearchDTO();
        searchDTO.setBklsFlag("0"); // 假设0表示非黑名单
        searchDTO.setCurrentPage(1);
        searchDTO.setPageSize(50);
        
        SearchParam<BidprcuDrugInfoSearchDTO> searchParam = new SearchParam<>(searchDTO);
        return esSearchService.search(searchParam, BidprcuDrugInfoSearchDTO.class);
    }

    /**
     * 复杂搜索示例：按药品名称、生产企业、剂型组合搜索
     */
    public PageList<BidprcuDrugInfoSearchDTO> complexSearch(String drugName, String manufacturer, String dosageForm) {
        BidprcuDrugInfoSearchDTO searchDTO = new BidprcuDrugInfoSearchDTO();
        searchDTO.setDrugName(drugName);
        searchDTO.setProdentpName(manufacturer);
        searchDTO.setDosformName(dosageForm);
        searchDTO.setCurrentPage(1);
        searchDTO.setPageSize(20);
        
        SearchParam<BidprcuDrugInfoSearchDTO> searchParam = new SearchParam<>(searchDTO);
        return esSearchService.search(searchParam, BidprcuDrugInfoSearchDTO.class);
    }

    /**
     * 跨索引搜索示例
     */
    public PageList<BidprcuDrugInfoSearchDTO> crossIndexSearch(String drugName, String... indices) {
        BidprcuDrugInfoSearchDTO searchDTO = new BidprcuDrugInfoSearchDTO();
        searchDTO.setDrugName(drugName);
        searchDTO.setCurrentPage(1);
        searchDTO.setPageSize(20);
        
        SearchParam<BidprcuDrugInfoSearchDTO> searchParam = new SearchParam<>(searchDTO);
        return esSearchService.search(searchParam, BidprcuDrugInfoSearchDTO.class, indices);
    }
} 